package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.util.Random;

import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactModificationTests extends TestBase {
	
	@Test(dataProvider = "randomValidContactGenerator")
	public void modifySomeContact(ContactData contact) {
		// save old state
		SortedListOf<ContactData> oldList = new SortedListOf<ContactData>();
		oldList.addAll(app.getModel().getContacts());
		
		Random rnd = new Random();
		int index = rnd.nextInt(oldList.size()-1);
				
		// action
		app.getContactHelper().modifyContact(index, contact);
		
		// save new state
		SortedListOf<ContactData> newList = app.getModel().getContacts();
				
		// compare states
		assertThat(newList, equalTo(oldList.without(index).withAdded(contact)));
		
		if (wantToCheck()) {
			if("yes".equals(app.getProperty("check.db"))) {
				assertThat(app.getModel().getContacts(), equalTo(app.getHibernateHelper().listContacts()));
			}
			
			if("yes".equals(app.getProperty("check.ui"))) {
				assertThat(app.getModel().getContacts(), equalTo(app.getContactHelper().getUiContacts()));
			}
		}
	}

}
